 

import pandas as pd
import numpy as np
from scipy import spatial
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'SimHei'


#数据读取
data = pd.read_csv('CityCoordinates.csv')
cityname = data['city']
city_condition = np.array(data[['x','y']]) #转换为数组
#距离矩阵
distance_matrix = spatial.distance.cdist(city_condition, city_condition, metric='euclidean') #以欧式距离计算得距离矩阵


#参数设置
AntCount = 50 #蚂蚁数量
alpha = 1  #信息素因子[1,4]
beta = 2  #启发函数因子[0,5]
rho = 0.1 #信息素挥发因子[0.2,0.5]
iter = 0  #迭代初始值
MAX_iter = 100  #最大迭代次数
Q = 1  #信息素常量
city_count = len(cityname) #城市数量


# 初始信息素矩阵，全是为1组成的矩阵
pheromonetable = np.ones((city_count, city_count))
  
# 候选集列表,存放蚂蚁的路径
candidate = np.zeros((AntCount, city_count)).astype(int)

# path_best存放的是相应的，每次迭代后的最优路径
path_best = np.zeros((MAX_iter, city_count)) 

# 存放每次迭代的最优距离
distance_best = np.zeros(MAX_iter) 
# 倒数矩阵
etable = 1.0 / distance_matrix

lengthaver = np.zeros(MAX_iter)  # 各代路径的平均长度
 